package cn.edu.njuit.springbootfilmreview.service.impl;

import cn.edu.njuit.springbootfilmreview.dto.AccountInfo;
import cn.edu.njuit.springbootfilmreview.service.AccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author guzy
 * @Description 用户token实现类
 * @Date 2022/5/24 8:18
 **/

@Component
public class UserTokenService {

//    thisisatoken.123.tokenend
    @Autowired
    AccountInfoService accountInfoService;

    /**
     *根据登录名生成令牌
     * @return
     */
    public String getToken(String login){
        return "thisisatoken."+login+".tokenend";
    }


    /**
     * 通过令牌生成User对象
     * @param token
     * @return
     */
    public AccountInfo getUser(String token){
        try{
            String login = token.split("\\.")[1];
            AccountInfo accountInfo = new AccountInfo();
            accountInfo = accountInfoService.getAccountByUserName(login);
            return accountInfo;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

